import { Button, Form, message } from 'antd';
import ProForm, {
  ModalForm,
  ProFormText,
  ProFormDigit,
  ProFormRadio,
  ProFormTextArea,
} from '@ant-design/pro-form';
import CategorySelect from '@/components/CategorySelect';
import UploadImage from '@/components/UploadImage';
import type { FormGoodsType } from './data';
import { updateGoods } from '../service';
const formLayout = {
  labelCol: { span: 8 },
  wrapperCol: { span: 16 },
};
interface UpdateFormProps {
  setRefreshCount: any;
  formVals: FormGoodsType;
}
const UpdateModalForm = (props: UpdateFormProps) => {
  const { formVals, setRefreshCount } = props;
  const [form] = Form.useForm();

  /**
   * 更新节点
   * @param fields
   */
  const handleUpdate = async (fields: FormGoodsType) => {
    const hide = message.loading('正在更新');
    try {
      const res = await updateGoods({
        ...formVals,
        ...fields,
        id: formVals.id,
      });
      hide();
      if (res.success) {
        setRefreshCount((count: any) => count + 1);
        message.success('更新成功');
      } else {
        message.error('更新失败,请重试');
      }
      return true;
    } catch (error) {
      hide();
      message.error('更新失败,请重试');
      return false;
    }
  };
  return (
    <ModalForm
      form={form}
      layout="horizontal"
      {...formLayout}
      title="编辑商品信息"
      modalProps={{
        onCancel: () => form.resetFields(),
      }}
      initialValues={{
        title: formVals.title,
        sub_title: formVals.sub_title,
        image_url: formVals.image_url,
        category_id: formVals.category_id,
        current_price: formVals.current_price,
        original_price: formVals.original_price,
        stock: formVals.stock,
        desc: formVals.desc,
        sales_volume: formVals.sales_volume,
        is_onShelf: formVals.is_onShelf,
        birth_place: formVals.birth_place,
        characteristic: formVals.characteristic,
      }}
      trigger={
        <Button size="small" type="primary">
          编辑
        </Button>
      }
      onFinish={async () => {
        form
          .validateFields()
          .then((fieldsValue) => {
            handleUpdate(fieldsValue);
          })
          .catch(() => {});
        return true;
      }}
    >
      <ProForm.Item
        key="image_url"
        name="image_url"
        label="图片"
        getValueFromEvent={(e) => {
          return e.response.url;
        }}
        rules={[{ required: true, message: '请上传图片' }]}
      >
        <UploadImage forValue="carsoul" />
      </ProForm.Item>
      <ProFormText
        key="title"
        name="title"
        label="商品名称"
        placeholder="商品名称"
        rules={[{ required: true, message: '请输入商品名称', min: 1, max: 12 }]}
        width={256}
      />
      <ProFormText
        key="sub_title"
        name="sub_title"
        label="商品副标题"
        placeholder="商品副标题"
        rules={[{ required: true, message: '请输入商品名称(1~20位)', min: 1, max: 12 }]}
        width={256}
      />
      <ProFormTextArea
        key="desc"
        name="desc"
        label="商品描述"
        fieldProps={{
          rows: 5,
        }}
        placeholder="请输入商品描述"
        rules={[{ required: true, message: '请输入商品描述' }]}
        width={256}
      />
      <ProFormText
        key="birth_place"
        name="birth_place"
        label="茶叶主产地"
        placeholder="请输入茶叶主产地"
        rules={[{ required: true, message: '请输入茶叶主产地' }]}
        width={256}
      />
      <ProFormTextArea
        key="characteristic"
        name="characteristic"
        label="茶叶描述"
        placeholder="请输入茶叶特色"
        rules={[{ required: true, message: '请输入茶叶特色' }]}
        width={256}
      />
      <ProFormDigit
        label="商品原价"
        key="original_price"
        name="original_price"
        rules={[{ required: true, message: '请输入商品原价' }]}
        width={256}
        min={1}
      />
      <ProFormDigit
        label="商品现价"
        key="current_price"
        name="current_price"
        rules={[{ required: true, message: '请输入商品现价' }]}
        width={256}
        min={1}
      />

      <ProFormDigit
        label="商品库存"
        name="stock"
        rules={[{ required: true, message: '请输入商品库存' }]}
        key="stock"
        width={256}
        min={1}
      />
      <ProForm.Item
        key="category_id"
        name="category_id"
        label="商品分类"
        rules={[{ required: true, message: '请选择商品分类' }]}
      >
        <CategorySelect placeholder=" 请选择商品分类" />
      </ProForm.Item>
      <ProFormRadio.Group
        name="is_onShelf"
        key="is_onShelf"
        label="是否上架"
        options={[
          {
            label: '是',
            value: 1,
          },
          {
            label: '否',
            value: 0,
          },
        ]}
      />
    </ModalForm>
  );
};
export default UpdateModalForm;
